package net.oschina.app.adapter;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import net.oschina.app.AppContext;
import net.oschina.app.R;
import net.oschina.app.bean.ChatMessage;
import net.oschina.app.emoji.InputHelper;
import net.oschina.app.widget.AvatarView;
import net.oschina.app.widget.MyLinkMovementMethod;
import net.oschina.app.widget.MyURLSpan;
import net.oschina.app.widget.TweetTextView;

import org.kymjs.kjframe.KJBitmap;
import org.kymjs.kjframe.bitmap.BitmapCallBack;

import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by yihao on 9/18/15.
 */
public class ChatMessageAdapter extends DateSortedAdapter {
    public ChatMessageAdapter(Activity activity, int timeInterval, int sortMode) {
        super(activity, timeInterval, sortMode);
    }

    public void addMessages(List<ChatMessage> messages){
        addDateItems(Arrays.asList(messages.toArray(new DateItem[0])));
    }

    public void addData(List data) {
//        addDateItems(Arrays.asList(data.toArray(new DateItem[0])));
    }

    @Override
    protected View getRealView(int position, View convertView,
                               final ViewGroup parent) {

        if(getItemViewType(position) == 0){
            final ChatMessage message = (ChatMessage) mSortedItems.get(position);
            int itemType = 0;
            if (message.getAuthorId() == AppContext.getInstance().getLoginUid()) {
                itemType = 1;
            }
            else{
                itemType = 0;
            }
            boolean needCreateView = false;
            ViewHolder vh = null;
            if (convertView == null) {
                needCreateView = true;
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            if (vh != null && (vh.type != itemType)) {
                needCreateView = true;
            }
            if (vh == null) {
                needCreateView = true;
            }

            if (needCreateView) {
                convertView = getLayoutInflater(parent.getContext()).inflate(
                        itemType == 0 ? R.layout.list_cell_chat_from
                                : R.layout.list_cell_chat_to, null);
                vh = new ViewHolder(convertView);
                vh.type = itemType;
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            vh.bindData(message);
            return convertView;

        }else{
            return getDateMarkView(position, convertView, parent);
        }

        // vh.name.setText(item.getAuthor());

//        vh.content.setMovementMethod(MyLinkMovementMethod.a());
//        vh.content.setFocusable(false);
//        vh.content.setDispatchToParent(true);
//        vh.content.setLongClickable(false);
//        Spanned span = Html.fromHtml(item.getContent());
//        span = InputHelper.displayEmoji(parent.getResources(), span);
//        vh.content.setText(span);
//        MyURLSpan.parseLinkText(vh.content, span);
//
//        vh.avatar.setAvatarUrl(item.getPortrait());
//        vh.avatar.setUserInfo(item.getAuthorId(), item.getAuthor());


    }


    public class ViewHolder{
        int type;
        @InjectView(R.id.iv_avatar)
        AvatarView avatar;
        @InjectView(R.id.tv_time)
        TextView time;
        @InjectView(R.id.tv_content)
        TweetTextView content;

        @InjectView(R.id.imageView)
        ImageView imageView;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }

        public void bindData(ChatMessage message){
            content.setMovementMethod(MyLinkMovementMethod.a());
            content.setFocusable(false);
            content.setDispatchToParent(true);
            content.setLongClickable(false);
            Spanned span = Html.fromHtml(message.getContent());
            span = InputHelper.displayEmoji(content.getResources(), span);
            content.setText(span);
            MyURLSpan.parseLinkText(content, span);

            avatar.setAvatarUrl(message.getPortrait());
            avatar.setUserInfo(message.getAuthorId(), message.getAuthor());

            if(message.getContent().endsWith(".jpg")|message.getContent().endsWith(".png")){
                imageView.setVisibility(View.VISIBLE);
                KJBitmap kjbitmap = new KJBitmap();
                kjbitmap.displayWithDefWH(imageView, message.getContent(),
                        new ColorDrawable(0x000000), new ColorDrawable(0x000000),
                        new BitmapCallBack() {
                            @Override
                            public void onPreLoad() {
                                super.onPreLoad();
                                //bar.setVisibility(View.VISIBLE);
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();
                                //bar.setVisibility(View.GONE);
                            }

                            @Override
                            public void onFailure(Exception arg0) {
                                AppContext.showToast(R.string.tip_load_image_faile);
                            }
                        });
                content.setVisibility(View.GONE);
            }
            else{
                content.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.GONE);
            }
        }
    }
}
